import { defineStore } from 'pinia';
import { ref } from 'vue';

type ThemeType = 'light' | 'dark';

export const useSettingStore = defineStore('setting', () => {
    // 1. 主题管理
    const theme = ref<ThemeType>((localStorage.getItem('theme') as ThemeType) || 'light');

    function toggleTheme() {
        theme.value = theme.value === 'light' ? 'dark' : 'light';
        applyTheme();
    }

    function applyTheme() {
        document.documentElement.setAttribute('data-theme', theme.value);
        // 适配 Element Plus 的暗黑模式
        if (theme.value === 'dark') {
            document.documentElement.classList.add('dark');
        } else {
            document.documentElement.classList.remove('dark');
        }
        localStorage.setItem('theme', theme.value);
    }
    
    // 初始化时应用一次主题
    applyTheme();

    // 2. 国际化管理 (后面会用到)
    const language = ref<string>(localStorage.getItem('language') || 'zh-cn');

    function setLanguage(lang: string) {
        language.value = lang;
        localStorage.setItem('language', lang);
        // 刷新页面以应用所有语言更改，或者使用更高级的 i18n.locale 切换
        window.location.reload(); 
    }


    return { theme, toggleTheme, language, setLanguage };
});
